package voloshyn.paliy.camera;

import android.os.Handler;
import android.os.Message;

public class ConnectHandler extends Handler {
    public static final int MESSAGE_DISCONNECT = 1;
    public static final int MESSAGE_DISCONNECT_ERROR = 2;
    public static final int MESSAGE_INPUT_STRING = 3;

    private OnConnectListener connectListener;

    protected ConnectHandler() {

    }

    protected void setOnConnectListener(OnConnectListener theConnectListener) {
	connectListener = theConnectListener;
    }

    @Override
    public void handleMessage(Message msg) {
	super.handleMessage(msg);
	if (connectListener == null) {
	    return;
	}

	switch (msg.what) {

	case MESSAGE_DISCONNECT: {
	    connectListener.disconnected();
	    break;
	}
	case MESSAGE_INPUT_STRING: {
	    String data = (String) msg.obj;
	    if (data != null) {
		connectListener.inputData(data);
	    }
	    break;
	}
	case MESSAGE_DISCONNECT_ERROR: {
	    connectListener.disconnectedError();
	    break;
	}
	}
    }

    protected void handle(int theKeyMessage, Object theObject) {
	Message message = new Message();
	message.what = theKeyMessage;
	message.obj = theObject;
	sendMessage(message);
    }

    protected void handle(int theKeyMessage) {
	handle(theKeyMessage, null);
    }
}
